package com.example.compose

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.blankj.utilcode.util.LogUtils
import com.example.compose.ui.theme.ComposeTheme
import com.example.compose.view.ui.ViewActivity

class MainActivity : ComponentActivity() {
    // 注册 ActivityResultLauncher 用于处理 TwoActivity 的返回结果
    private lateinit var twoActivityLauncher: ActivityResultLauncher<Intent>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 注册 ActivityResultLauncher
        twoActivityLauncher = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) { result ->
            if (result.resultCode == RESULT_OK) {
                val data = result.data?.getStringExtra("RESULT_DATA")
                // 处理从 TwoActivity 返回的数据
                LogUtils.e("从 TwoActivity 获取的数据: $data")
            }
        }

        enableEdgeToEdge()
        setContent {
            ComposeTheme {
                // 数据源（提前定义好）
                val items = listOf("view")
                // === 首页：显示列表 ===
                Column(modifier = Modifier.fillMaxSize()) {
                    Text(
                        text = "列表",
                        style = MaterialTheme.typography.displaySmall,
                        modifier = Modifier.padding(16.dp)
                    )

                    LazyColumn(modifier = Modifier.fillMaxSize()) {
                        items(items) { item ->
                            ListItem(
                                text = item,
                                onClick = {
                                    // 跳转到 TwoActivity 并携带参数
                                    val intent = Intent(this@MainActivity, ViewActivity::class.java)
                                    intent.putExtra("SELECTED_ITEM", item)
                                    twoActivityLauncher.launch(intent)
                                },
                                modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
                            )
                        }
                    }
                }
            }
        }
    }
}

// 自定义列表项（使用 Row, Column, Box 示例）
@Composable
fun ListItem(text: String, onClick: () -> Unit, modifier: Modifier = Modifier) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(12.dp)
            .background(Color.LightGray.copy(alpha = 0.1f)),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 左边：一个彩色小方块（Box）
        Box(
            modifier = Modifier
                .size(40.dp)
                .background(Color.Green)
        )

        Spacer(modifier = Modifier.width(16.dp))

        // 中间：文字用 Column（虽然只有一个 Text，演示用）
        Column {
            Text(text = text, style = MaterialTheme.typography.bodyMedium)
            Text(
                text = "点击查看详情",
                style = MaterialTheme.typography.bodySmall,
                color = Color.Gray
            )
        }

        Spacer(modifier = Modifier.weight(1f)) // 推右边

        // 右边：箭头
        Icon(
            imageVector = Icons.Default.ArrowForward,
            contentDescription = "更多",
            tint = Color.Gray
        )
    }
}